<template>
  <div class="menu-list">
    <el-card class="box-card">
      <div slot="header">
        <el-button @click="$router.push({ name: 'add-or-edit' })"
          >添加菜单</el-button
        >
      </div>
      <el-table :data="menus" style="width: 100%">
        <el-table-column prop="id" label="编号" width="180"> </el-table-column>
        <el-table-column prop="name" label="菜单名称" width="180">
        </el-table-column>
        <el-table-column prop="level" label="菜单级数" width="180">
          <template slot-scope="scope"> {{ scope.row.level + 1 }}级 </template>
        </el-table-column>
        <el-table-column prop="icon" label="前端图标" width="180">
        </el-table-column>
        <el-table-column prop="name" label="排序" width="180">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="editMenu(scope.row)">编辑</el-button>
            <el-button type="text" @click="deleteMenu(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getMenuList, deleteMenu } from '@/service/menu'
export default {
  name: 'MenuList',
  data () {
    return {
      menus: []
    }
  },
  created () {
    this.loadMenuList()
  },
  methods: {
    // 加载菜单
    async loadMenuList () {
      const { data } = await getMenuList()
      if (data.code === '000000') {
        this.menus = data.data
      }
    },
    // 添加菜单
    editMenu (row) {
      // console.log(row)
      this.$router.push({ name: 'add-or-edit', params: { id: row.id } })
    },
    // 删除菜单
    deleteMenu (row) {
      // console.log(row)
      this.$confirm('即将删除该菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteMenu(row.id)
        if (data.code === '000000') {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadMenuList()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-card__header {
  text-align: left;
}
</style>
